package com.itheima.service.impl;

import com.itheima.mapper.ReportMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl  implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    Calendar cal=Calendar.getInstance();

    //获取会员统计
    @Override
    public Map getMemberReportLast12Month() {
        //定义两个list集合，用来存放，每一个月，和想对应的注册会员数量
        List<String> yearAndMonth =new ArrayList<>();
        List<Long> memberCounts =new ArrayList<>();
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历往前推12个月
        calendar.add(Calendar.MONTH,-12);

        for (int i=0;i<12;i++){
            //获取每个月时间
            Date time = calendar.getTime();
            //转换为想要的日期格式
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            String month = sdf.format(time);
            yearAndMonth.add(month);
            //获取每个月对应会员的数量
            String startDate=month+"-1";
            String endDate=month+"-31";
            Long num=reportMapper.findMemberCountsByDate(startDate,endDate);
            memberCounts.add(num);

            //将当前月向后推一个月,循环一次月份加一
            calendar.add(Calendar.MONTH,1);
        }

        Map map=new HashMap();
        map.put("months",yearAndMonth);
        map.put("memberCounts",memberCounts);
        return map;
    }


    //获取套餐统计
    @Override
    public Map getSetmealReport() {
        List<Map> list=setMealMapper.getSetmealReport();
        List<String> name=new ArrayList<>();
        for (Map map : list) {
            String name1 = (String) map.get("name");
            name.add(name1);
        }

        Map map=new HashMap();
        map.put("setmealNames",name);
        map.put("setmealCounts",list);
        return map;
    }

    //获取运营报表
    @Override
    public Map getBusinessReport() {
        Map map=new HashMap();
        try {

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH)+1;
            int day = cal.get(Calendar.DATE);
            String today=year+"-"+month+"-"+day;
            //今日新增会员数
            map.put("todayNewMember",reportMapper.findMemberCountToday(today));
            //总会员数
            map.put("totalMember",reportMapper.findMemberCount());

            //获取当前日期的周一
            String curFirstDayThisWeek=DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本周的会员数
            map.put("thisWeekNewMember",reportMapper.findMemberCountWeek(curFirstDayThisWeek));

            //获取当前日期的月份的一号
            String curFirstDayThisMonth=DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本月的会员数
            map.put("thisMonthNewMember",reportMapper.findMemberCountMonth(curFirstDayThisMonth));

            //获取今天预约数
            map.put("todayOrderNumber",reportMapper.findOrderNumberToday(today));

            //获取今日到诊数
            map.put("todayVisitsNumber",reportMapper.findOrderOrderStatus(today));

            //本周预约数
            map.put("thisWeekOrderNumber",reportMapper.findOrderNumberThisWeek(curFirstDayThisWeek));

            //本周到诊数
            map.put("thisWeekVisitsNumber",reportMapper.findVisitsNumber(curFirstDayThisWeek));

            //本月预约数
            map.put("thisMonthOrderNumber",reportMapper.findOrderNumberThisMonth(curFirstDayThisMonth));

            //本月到诊数
            map.put("thisMonthVisitsNumber",reportMapper.findMonthVisitsNumber(curFirstDayThisMonth));

            //热门套餐
            List<Map> listMap=reportMapper.findHotSetmeal();
            map.put("hotSetmeal",listMap);
            map.put("reportDate",today);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return  map;
    }
}
